
% for best performance, run batchSelectROI first to draw masks on the
% movies

function [center,radius,metric] = L_trackeye2p(mov,bw)

%% set some basic parameters for the circle:
radiusrangeLow = [2,20];
radiusrangeHigh = [5,20];

if nargin < 2 || isempty(bw)
    bw = logical(true([size(mov,1),size(mov,2)]));
end
doplot = false;
W = warning('off','all');
%% track eye positions and plot if want
if doplot
    plotfig = figure;
end
center = nan(size(mov,3),2);
radius = nan(size(mov,3),1);
metric = nan(size(mov,3),1);
for n = 1:size(mov,3)
    im = mov(:,:,n);
    im(~bw) = 0;
    [c,r,m] = imfindcircles(im,radiusrangeHigh);
    if isempty(c)
        [c,r,m] = imfindcircles(im,radiusrangeLow);
    end
    if isempty(c)
        continue;
    end
    center(n,:) = c(1,:);radius(n) = r(1);metric(n) = m(1);
    if doplot
        figure(plotfig)
        imagesc(im);
        hold on
        viscircles(c(1,:),r(1),'EdgeColor','b');
        hold off
        title(n)
        pause(.01);
    end
end

W = warning('on','all');